<template>
    <div>
        <page-header-nav :content="pageTitle" :need-back="true" :need-underline="true"></page-header-nav>
        <medication-form
            :id="this.id"
            :name="this.name"
            :dosage="this.dosage"
            :start-date="this.startDate"
            :end-date="this.endDate"
            :reminder-item-list="reminderItemList"
        />
    </div>
</template>

<script>
import PageHeaderNav from "@/components/Publlic/PageHeaderNav.vue";
import MedicationForm from "@/components/MedicineClock/MedicationForm.vue";

export default {
    components: {
        MedicationForm,
        PageHeaderNav,
    },
    data() {
        return {
            id: "",
            name: "",
            dosage: "",
            startDate: new Date(new Date().setHours(0, 0, 0, 0)),
            endDate: new Date(new Date().setHours(0, 0, 0, 0)),
            reminderItemList: [],
            pageTitle: null,
        }
    },

    created() {
        // 路由地址切片，获取medicineId
        const routeList = this.$route.fullPath.split("/")
        const editMedicineIndex = routeList.indexOf("editMedicineView")

        // medicineId在路由结尾存在，请求指定药品的信息
        if (editMedicineIndex < routeList.length - 1) {
            this.pageTitle = "修改药品提醒"
            const medicineId = routeList[editMedicineIndex + 1]
            this.request.post("/medicineReminder/getReminder/", {
                id: medicineId,
            }).then((response) => {
                const status = response["code"] === 200

                // 获取失败
                if (!status) {
                    setTimeout(() => {
                        this.$router.back()
                    }, 500)
                    return
                }

                // 获取成功，回写数据
                const medicine = response["data"]["medicine"]
                this.id = medicine.id
                this.name = medicine.name
                this.dosage = medicine.dosage
                this.startDate = this.$public.timeStampToDate(medicine.startTimeStamp)
                this.endDate = this.$public.timeStampToDate(medicine.endTimeStamp)
                this.reminderItemList = medicine.reminderItemList.map(reminderItem => {
                    return {id: reminderItem.id, timeDate: this.$public.timeStampToDate(reminderItem.timeStamp)}
                })
            })
        } else {
            this.pageTitle = "新增药品提醒"
        }
    }
}
</script>

<style lang="less" scoped>
</style>